package com.sharker.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sharker.R;
import com.sharker.models.courseexam.Question;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 1.类的用途：
 * 2.作者：${赵渊} on 2017/4/14 20:28
 */
public class CourseExamAdapter extends RecyclerView.Adapter<CourseExamAdapter.MyViewHolder> {


    private final Context context;
    private final List<Question> question;
    private String testResult;

    public String getTestResult() {
        return testResult;
    }

    public CourseExamAdapter(Context context, List<Question> question) {
        this.context = context;
        this.question = question;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.review_test_recyitem, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.answer.setText(question.get(position).problem);
        String[] options = question.get(position).option;
        holder.rbAnswer1.setText(options[0]);
        holder.rbAnswer2.setText(options[1]);
        holder.rbAnswer3.setText(options[2]);
        if (options.length > 3) {
            holder.rbAnswer4.setText(options[3]);
        }
        StringBuilder builder = new StringBuilder();
        if(holder.rbAnswer1.isChecked()){
            builder.append("A");
        }else if(holder.rbAnswer2.isChecked()){
            builder.append("B");
        }else if(holder.rbAnswer3.isChecked()){
            builder.append("C");
        }else if(holder.rbAnswer3.isChecked()){
            builder.append("D");
        }
        testResult = builder.toString();
    }


    @Override
    public int getItemCount() {
        return question.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.answer)
        TextView answer;
        @BindView(R.id.rgAnswer)
        RadioGroup rgAnswer;
        @BindView(R.id.rbAnswer1)
        RadioButton rbAnswer1;
        @BindView(R.id.rbAnswer2)
        RadioButton rbAnswer2;
        @BindView(R.id.rbAnswer3)
        RadioButton rbAnswer3;
        @BindView(R.id.rbAnswer4)
        RadioButton rbAnswer4;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(itemView);
        }
    }

}
